package com.bdqn.zhang.service.impl;

import com.bdqn.zhang.dao.CatRepository;
import com.bdqn.zhang.dto.CatDto;
import com.bdqn.zhang.dto.CatProjection;
import com.bdqn.zhang.pojo.Cat;
import com.bdqn.zhang.service.CatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

/**
 * <Description> <br>
 *
 * @author tao<br>
 * @version 1.0<br>
 * @taskId: <br>
 * @createDate 2019/10/17 12:17 <br>
 * @see com.bdqn.zhang.service.impl <br>
 */
@Service("catService")
public class CatServiceImpl implements CatService {

    @Autowired
    private CatRepository repository;

    @Override
    public Cat insert(Cat cat) {
        return repository.save(cat);
    }

    @Override
    public List<Cat> insert(List<Cat> list) {
        return repository.saveAll(list);
    }
    @Override
    public void delete(Integer id) {
        repository.deleteById(id);
    }

    @Override
    public void delete(Cat cat) {
        repository.delete(cat);
    }

    @Override
    public void delete(List<Cat> list) {
        repository.deleteAll(list);
    }

    @Override
    public void deleteAll() {
        repository.deleteAll();
    }

    @Override
    public Cat update(Cat cat) {
        return repository.save(cat);
    }

    @Override
    public List<Cat> update(List<Cat> list){
        return repository.saveAll(list);
    }

    @Override
    public Boolean exists(Integer id){
        return repository.existsById(id);
    }

    @Override
    public Long count() {
        return repository.count();
    }

    @Override
    public Cat queryById(Integer id) {
        Cat cat = new Cat();
        cat.setId(id);
        Example<Cat> example = Example.of(cat);
        Optional<Cat> catOptional = repository.findOne(example);
        if (catOptional.isPresent()) {
            return catOptional.get();
        } else {
            return null;
        }
    }

    @Override
    public List<Cat> queryAll(){
        return repository.findAll();
    }

    @Override
    public List<Cat> queryAllSort(String fieldName){//排序
        Sort sort = Sort.by(Sort.Direction.DESC,"id");
        return repository.findAll(sort);
    }

    @Override
    public Page<Cat> queryAllPage(Integer page,Integer size){//分页
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page, size, sort);
        return repository.findAll(pageable);
    }

    //----------------------------以上是JpaRepository已经实现好的基本增删改查-----------------
    @Override
    public Long queryMaxAge() {
        return repository.maxAge();
    }

    @Override
    public List<Cat> queryByNameLike(String name){
        return repository.findByNameLike("%"+name+"%");
    }

    @Override
    public List<Cat> queryByName(String name){
        return repository.queryByname(name);
    }

    @Override
    public List<Cat> queryByNameSQL(String name){
        return repository.queryBynameSQL(name);
    }

    @Override
    public List<Cat> queryNameLikeAllSort(String nameLike,String fieldName){//带条件的排序
        // Sort sort = new Sort(Sort.Direction.DESC, "id");
        // Pageable pageable = new PageRequest(page, size, sort);
        Sort sort = Sort.by(Sort.Direction.DESC, fieldName,"id");
        return repository.findByNameLike("%"+nameLike+"%", sort);
    }

    @Override
    public Page<Cat> queryNameLikeAllPage(String nameLike, Integer page, Integer size){//带条件的分页
        Sort sort = Sort.by(Sort.Direction.ASC, "id");
        Pageable pageable = PageRequest.of(page, size, sort);
        return repository.findByNameLike("%"+nameLike+"%", pageable);
    }

    @Override
    public Collection<CatProjection> findCatNameAndBirthdayById(Integer id) {
        return repository.findCatNameAndBirthdayById(id);
    }

    @Override
    public Collection<CatDto> findCatNameAndBirthdayById2(Integer id) {
        return repository.findCatNameAndBirthdayById2(id);
    }
}
